//
//  PopRippleButton.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/13.
//



import UIKit
import pop

class PopRippleButton: UIButton {
    
    // 水波纹颜色
    var rippleColor: UIColor = UIColor(red: 0.2, green: 0.5, blue: 0.7, alpha: 0.5)
    // 水波纹动画时间
    var rippleDuration: TimeInterval = 0.8
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    
    private func setup() {
        // 设置按钮为圆角
        layer.cornerRadius = 8
        clipsToBounds = true
        backgroundColor = .systemBlue
        setTitleColor(.white, for: .normal)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
        // 获取触摸点
        guard let touch = touches.first else { return }
        let touchPoint = touch.location(in: self)
        
        // 创建水波纹视图
        let rippleView = createRippleView(at: touchPoint)
        addSubview(rippleView)
        
        // 使用POP执行水波纹动画
        animateRippleViewWithPOP(rippleView)
    }
    
    private func createRippleView(at point: CGPoint) -> UIView {
        // 计算水波纹大小
        let size = max(bounds.width, bounds.height) * 2
        let rippleView = UIView(frame: CGRect(x: 0, y: 0, width: size, height: size))
        
        // 设置水波纹属性
        rippleView.backgroundColor = rippleColor
        rippleView.layer.cornerRadius = size / 2
        rippleView.center = point
        rippleView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        rippleView.alpha = 1
        
        return rippleView
    }
    
    private func animateRippleViewWithPOP(_ rippleView: UIView) {
        // 创建缩放动画
        let scaleAnimation = POPBasicAnimation(propertyNamed: kPOPViewScaleXY)
        scaleAnimation?.toValue = NSValue(cgPoint: CGPoint(x: 1, y: 1))
        scaleAnimation?.duration = rippleDuration
        
        // 创建透明度动画
        let alphaAnimation = POPBasicAnimation(propertyNamed: kPOPViewAlpha)
        alphaAnimation?.toValue = 0
        alphaAnimation?.duration = rippleDuration
        
        // 设置动画完成回调
        scaleAnimation?.completionBlock = { [weak self, weak rippleView] _, _ in
            rippleView?.removeFromSuperview()
        }
        
        // 添加动画到视图
        rippleView.pop_add(scaleAnimation, forKey: "scaleAnimation")
        rippleView.pop_add(alphaAnimation, forKey: "alphaAnimation")
    }
}

extension PopRippleButton {
    /// 配置按钮的通用样式
    /// - Parameters:
    ///   - title: 按钮标题
    ///   - state: 按钮状态（默认 `.normal`）
    ///   - fontSize: 字体大小
    ///   - cornerRadius: 圆角半径
    ///   - backgroundColor: 背景颜色
    ///   - tintColor: 文本或图标颜色
    ///   - isEnabled: 是否可用
    func configurePopButton(
        type:UIButton.ButtonType = .system,
        title: String,
        fontSize: CGFloat = 16,
        cornerRadius: CGFloat = 8,
        backgroundColor: UIColor = .systemBlue,
        tintColor: UIColor = .white,
        labelFont: UIFont = .adaptedBoldFont(size: 14),
        isEnabled: Bool = true
    ) {
//        self.buttonType = type,
        self.setTitle(title, for: .normal)
        self.titleLabel?.font = UIFont.systemFont(ofSize: fontSize, weight: .medium)
        self.layer.cornerRadius = cornerRadius
        self.backgroundColor = backgroundColor
        self.tintColor = tintColor
        self.titleLabel?.font = labelFont
        self.isEnabled = isEnabled
    }
    
    /// 快速创建一个配置好的 UIButton
    static func createRipple(
        type : UIButton.ButtonType,
        title: String,
        fontSize: CGFloat = 16,
        cornerRadius: CGFloat = 8,
        backgroundColor: UIColor = .systemBlue,
        tintColor: UIColor = .white,
        labelFont : UIFont = .adaptedBoldFont(size: 15),
        isEnabled: Bool = true
    ) -> PopRippleButton {
        let button = PopRippleButton()
        button.configurePopButton(
            type: type,
            title: title,
            fontSize: fontSize,
            cornerRadius: cornerRadius,
            backgroundColor: backgroundColor,
            tintColor: tintColor,
            labelFont: labelFont,
            isEnabled: isEnabled
        )
        return button
    }
}
